<script type="text/javascript">
    let dtree;
    layui.use(['index', 'dtree'], function () {
        dtree = layui.dtree;
        $.ajax({
            data: {
                industries: '{{$company->industries}}',
                tags: '{{$company->tags}}'
            },
            url: "{{route('admin.base.company.industry')}}",
            success: function (data) {
                let industry_data = data.industries;
                let tag_data = data.tags;
                initTree('industryTree', industry_data, '所属行业', 'industries');
                initTree('tagTree', tag_data, '企业标签', 'tags');
            }
        });
    });

    function initTree(id, datas, title, input_id) {
        dtree.renderSelect({
            elem: '#' + id,
            select: true,
            data: datas,
            checkbar: true,
            checkbarType: "self",
            selectTips: title,
            dataFormat: "list", //配置data的风格为list
            width: "100%",
            checkbarData: "choose",
            checkbarFun: {
                chooseDone: function (cheched) {
                    let types_array = [];
                    let texts_array = [];
                    cheched.forEach(function (item) {
                        types_array.push(item.nodeId);
                        texts_array.push(item.context);
                    });
                    $('#' + input_id).val(types_array.join(','));
                    $('input[dtree-id="' + id + '"]').val(texts_array.join(','));
                }
            }
        });
        let texts_array = [];
        datas.forEach(function (item) {
            if (item.checkArr == 1) {
                texts_array.push(item.title);
            }
        });
        $('input[dtree-id="' + id + '"]').val(texts_array.join(','));
    }

</script>
